return r.remove(QRegExp(regex));
}
-// csv_stringtrim() - trim whitespace and leading and trailing
-// enclosures (quotes)
+// csv_stringtrim() - trim whitespace and leading and trailing
+// enclosures (quotes)
// returns a copy of the modified string
// usage: p = csv_stringtrim(string, "\"", 0)
char*
return (tmp);
}
-// Is this really the replacement for the above?
+// Is this really the replacement for the above?
QString
-csv_stringtrim(const QString& source, const QString& enclosure)
+csv_stringtrim(const QString& source, const QString& enclosure)
{
QString r = source;
r.replace(enclosure, "");
is_internal = false;
ifield_ct = ofield_ct = 0;
extension = description = nullptr;
-// xcsv_file_init();
+// xcsv_file_init();
}
-void validate_fieldmap(field_map_t* fmp, bool is_output) {
+static void validate_fieldmap(const field_map_t* fmp, bool is_output) {
QString qkey = fmp->key;
QString qval = fmp->val;
QString qprintfc = fmp->printfc;
if (qkey.isEmpty()) {
- Fatal() << MYNAME << ": xcsv style is missing" <<
+ Fatal() << MYNAME << ": xcsv style is missing" <<
(is_output ? "output" : "input") << "field type.";
}
if (!fmp->val) {
/* usage: xcsv_ifield_add("DESCRIPTION", "", "%s") */
/*****************************************************************************/
void
-xcsv_ifield_add(char* key, char* val, char* pfc)
+xcsv_ifield_add(const char* key, const char* val, const char* pfc)
{
field_map_t* fmp = (field_map_t*) xcalloc(sizeof(*fmp), 1);
struct xt_mapping* xm = Perfect_Hash::in_word_set(key, strlen(key));
/* usage: xcsv_ofield_add("LAT_DECIMAL", "", "%08.5lf") */
/*****************************************************************************/
void
-xcsv_ofield_add(char* key, char* val, char* pfc, int options)
+xcsv_ofield_add(const char* key, const char* val, const char* pfc, int options)
{
field_map_t* fmp = (field_map_t*) xcalloc(sizeof(*fmp), 1);
struct xt_mapping* xm = Perfect_Hash::in_word_set(key, strlen(key));
return writetime(format, t.toTime_t(), gmt);
}
-QString
+QString
writehms(const char* format, time_t t, int gmt)
{
static struct tm no_time = tm();
/* no-op */
}
+// return |original| after performing token replacement.
+static QString
+xcsv_replace_tokens(const QString& original) {
+ QString replacement = original;
+ // Don't do potentially expensive replacements if token prefix
+ // isn't present;
+ if (original.contains("__")) {
+ struct tm tm;
+ time_t my_time = gpsbabel_time;
+ if (my_time == 0) { /* testo script ? */
+ tm = *gmtime(&my_time);
+ } else {
+ tm = *localtime(&my_time);
+ }
+
+ replacement.replace("__FILE__", xcsv_file.fname);
+ replacement.replace("__VERSION__", my_time == 0 ? "" : gpsbabel_version);
+
+ QDateTime dt = QDateTime::fromTime_t(my_time);
+ dt = dt.toTimeSpec(Qt::UTC);
+
+ QString dts = dt.toString("ddd MMM dd hh:mm:ss yyyy");
+ replacement.replace("__DATE_AND_TIME__", dts);
+
+ QString d = dt.toString("MM/dd/yyyy");
+ replacement.replace("__DATE__", d);
+
+ QString t = dt.toString("hh:mm:ss");
+ replacement.replace("__TIME__", t);
+ }
+ return replacement;
+}
+
/*****************************************************************************/
/* xcsv_data_write(void) - write prologues, spawn the output loop, and write */
/* epilogues. */
}
/* output prologue lines, if any. */
- foreach(const QString& line, xcsv_file.prologue) {
- // If the XCSV description contains weird characters (like sportsim)
- // this is where they get lost.
- QString cout = line;
-
- // Don't do potentially expensive replacements if token prefix
- // isn't present;
- if (cout.contains("__")) {
- cout.replace("__FILE__", xcsv_file.fname);
- cout.replace("__VERSION__", time == 0 ? "" : gpsbabel_version);
-
- QDateTime dt = QDateTime::fromTime_t(time);
- dt = dt.toTimeSpec(Qt::UTC);
-
- QString dts = dt.toString("ddd MMM dd hh:mm:ss yyyy");
- cout.replace("__DATE_AND_TIME__", dts);
-
- QString d = dt.toString("MM/dd/yyyy");
- cout.replace("__DATE__", d);
-
- QString t = dt.toString("hh:mm:ss");
- cout.replace("__TIME__", t);
- }
- *xcsv_file.stream << cout << xcsv_file.record_delimiter;
+ for (auto line : xcsv_file.prologue) {
+ QString line_to_write = xcsv_replace_tokens(line);
+ *xcsv_file.stream << line_to_write << xcsv_file.record_delimiter;
}
if ((xcsv_file.datatype == 0) || (xcsv_file.datatype == wptdata)) {
}
/* output epilogue lines, if any. */
- foreach(const QString& ogp, xcsv_file.epilogue) {
- *xcsv_file.stream << ogp << xcsv_file.record_delimiter;
+ for (auto line : xcsv_file.epilogue) {
+ QString line_to_write = xcsv_replace_tokens(line);
+ *xcsv_file.stream << line_to_write << xcsv_file.record_delimiter;
}
}
#endif
xcsv_epilogue_add(char*);
void
-xcsv_ifield_add(char*, char*, char*);
+xcsv_ifield_add(const char*, const char*, const char*);
void
-xcsv_ofield_add(char*, char*, char*, int options);
+xcsv_ofield_add(const char*, const char*, const char*, int options);
void
xcsv_destroy_style(void);
const char*
-xcsv_get_char_from_constant_table(char* key);
+xcsv_get_char_from_constant_table(const char* key);
/****************************************************************************/
/* types required for various xcsv functions */
#define OPTIONS_OPTIONAL 3
typedef struct field_map {
queue Q;
- char* key;
- char* val;
- char* printfc;
+ const char* key;
+ const char* val;
+ const char* printfc;
int hashed_key;
int options;
} field_map_t;
queue ifield; /* input field mapping */
queue* ofield; /* output field mapping */
+ QList<field_map> ifields;
+ QList<field_map> ofields;
int ifield_ct; /* actual # of ifields */
int ofield_ct; /* actual # of ofields */
QTextCodec* codec;
QString fname; /* ptr to filename of above. */
- char* description; /* Description for help text */
- char* extension; /* preferred filename extension (for wrappers)*/
+ const char* description; /* Description for help text */
+ const char* extension; /* preferred filename extension (for wrappers)*/
short_handle mkshort_handle;/* handle for mkshort() */
ff_type type; /* format type for GUI wrappers. */